package com.ganbo.thread.producer_consumer.single;

/**
 * 消费者
 *
 * @Author: ganbo
 * @Date: 2020/5/20 16:42
 */
public class Consumer extends Thread {

    private Object lock;

    public Consumer(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                if (ValueObject.value == null) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println(Thread.currentThread().getName() + "消费的值是:" + ValueObject.value);
                    ValueObject.value = null;
                    lock.notifyAll();

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
